using System;
using System.IO;

namespace Atomic.Libraries.Compression
{
	public static class BZip2
	{
		public static void Compress(Stream inputStream, Stream outputStream, int level) 
		{
			SharpZipLib.BZip2.Compress(inputStream, outputStream, false, level);
		}

		public static byte[] Compress(byte[] input, int level)
		{
			byte[] output;
			using (MemoryStream outputStream = new MemoryStream())
			{
				using (MemoryStream inputStream = new MemoryStream(input))
				{
					Compress(inputStream, outputStream, level);
					inputStream.Close();
				}
				output = outputStream.ToArray();
				outputStream.Close();
			}

			return output;
		}

		public static void Decompress(Stream inputStream, Stream outputStream) 
		{
			SharpZipLib.BZip2.Decompress(inputStream, outputStream, false);
		}

		public static byte[] Decompress(byte[] input)
		{
			byte[] output;
			using (MemoryStream outputStream = new MemoryStream())
			{
				using (MemoryStream inputStream = new MemoryStream(input))
				{
					Decompress(inputStream, outputStream);
					inputStream.Close();
				}
				output = outputStream.ToArray();
				outputStream.Close();
			}

			return output;
		}
	}
}
